﻿using mozhi.smarterp.Finance.Basedatas.Repository;
using mozhi.smarterp.Finance.Basedatas;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using mozhi.smarterp.Finance.Banks.Repository;
using Volo.Abp.EntityFrameworkCore;
using System.Threading.Tasks;
using mozhi.smarterp.Finance.Querys;
using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Finance.Accounts;
using System.Linq;

namespace mozhi.smarterp.Finance.Banks
{
    public class BankTypeRepository : EfCoreRepository<FinanceDbContext, BankType, Guid>, IBankTypeRepository
    {
        public BankTypeRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<List<BankType>> FindAsync(PageQuery query)
        {
            var bankTypes = await DbContext.Set<BankType>().AsNoTracking()
              .WhereIf(!query.Keyword.IsNullOrEmpty(), x => x.Name.Contains(query.Keyword))
              .WhereIf(query.Ids != null && query.Ids.Count > 0, x => query.Ids.Contains(x.Id))
              .WhereIf(query.Activated.HasValue, x => x.Activated == query.Activated.Value)
              .Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize)
              .ToListAsync();

            return bankTypes;
        }

        public async Task<BankType> FindAsync(Guid id)
        {
            return await DbContext.Set<BankType>().AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
        }

        public async Task<BankType> GetAsync(Guid id)
        {
            return await DbContext.Set<BankType>().FirstOrDefaultAsync(x => x.Id == id);
        }

        public async Task<int> GetCountAsync(PageQuery query)
        {
            int count = await DbContext.Set<BankType>().AsNoTracking()
                 .WhereIf(!query.Keyword.IsNullOrEmpty(), x => x.Name.Contains(query.Keyword))
                 .WhereIf(query.Activated.HasValue, x => x.Activated == query.Activated.Value)
                 .WhereIf(query.ExclusionId.HasValue, x => x.Id != query.ExclusionId)
                 .CountAsync();

            return count;
        }
    }
}
